module namespace f = "http://owl.cs.manchester.ac.uk/fones#";
declare namespace owl = "http://www.w3.org/2002/07/owl#";
declare namespace restxq = "http://exquery.org/ns/restxq";

declare variable $f:db := db:open("OWLDB");
declare variable $f:ontologies := $f:db//owl:Ontology;
declare variable  $f:nl := '&#xD;&#xA;';

(:function to count Logical Axioms
  @param Individual Ontology or a Database
:)

declare function f:countLogicalAxioms($ont){
  count(f:fetchLogicalAxiom($ont))
};

(:function to fetch the Logical Axioms
  @param Individual Ontology or a Database
:)

declare function f:fetchLogicalAxiom($ont){
  $ont//owl:SubClassOf,$ont//owl:FunctionalObjectProperty,$ont//owl:EquivalentClasses,$ont//owl:DisjointClasses,$ont//owl:DisjointUnion,$ont//owl:SubObjectPropertyOf,                        $ont//owl:ObjectPropertyChain,$ont//owl:EquivalentObjectProperties,$ont//owl:DisjointObjectProperties,$ont//owl:ObjectPropertyDomain,$ont//owl:ObjectPropertyRange,                         $ont//owl:InverseObjectProperties,$ont//owl:FunctionalObjectProperty,$ont//owl:InverseFunctionalObjectProperty,$ont//owl:ReflexiveObjectProperty,                                           $ont//owl:IrreflexiveObjectProperty,$ont//owl:SymmetricObjectProperty,$ont//owl:AsymmetricObjectProperty,$ont//owl:TransitiveObjectProperty,                                                $ont//owl:SubDataPropertyOf,$ont//owl:EquivalentDataProperties,$ont//owl:DisjointDataProperties,$ont//owl:DataPropertyDomain,$ont//owl:DataPropertyRange,                                   $ont//owl:FunctionalDataProperty,$ont//owl:DatatypeDefinition,$ont//owl:HasKey,$ont//owl:SameIndividual,$ont//owl:DifferentIndividuals,$ont//owl:ClassAssertion,                            $ont//owl:ObjectPropertyAssertion,$ont//owl:NegativeObjectPropertyAssertion,$ont//owl:DataPropertyAssertion,$ont//owl:NegativeDataPropertyAssertion
};


declare function f:substring-after-last-match
  ( $arg as xs:string? ,$regex as xs:string )  as xs:string {
      replace($arg,concat('^.*',$regex),'')
 };
 
declare %output:media-type("text/html")
     %restxq:path("fones/home")
     %restxq:GET
     function f:displayAxiomCount(){
        <html>
          <head>
            <title>Ontology Repository</title>
           </head>
           <body>
            <h2>Ontology Repository</h2>
            <h3>Ontologies</h3>
            <form action = "filter" method="get" value="filter">
              Filter Key<input type = "text" name ="filterKey" />  
              Filter Condition<input type = "text" name ="filterCondition" />  
              Filter Value<input type = "text" name ="filterValue" />  <br/>
              <input type="Submit" id ="submitcondition"/><br/>
            </form>
            <table>
              <tr>
                <th>Ontology Count  </th>
                <th>Logical Axioms</th>
              </tr>
              <tr>
                <td>{count($f:ontologies)}</td>
                <td>{f:countLogicalAxioms($f:db)}</td>
              </tr>
            </table>
            
            <h2>Details</h2>
             <table>
                <tr>
                  <th>Ontology</th>
                  <th>Number of Logical Axioms</th>
                </tr>
                  {for $ont in $f:ontologies
                    return
                      <tr>
                        <td>
                         {fn:replace(f:substring-after-last-match($ont//@ontologyIRI, '/'),'#',' ')}
                        </td>
                        <td>
                         { f:countLogicalAxioms($ont) }
                        </td>
                      </tr>
                   }
              </table>
              </body>
             </html>
};


declare   %output:media-type("text/html")
          %restxq:query-param("filterKey", "{$filterKey}")
          %restxq:query-param("filterCondition", "{$condition}")
          %restxq:query-param("filterValue", "{$value}")
          %restxq:path("fones/filter/") 
          %restxq:GET
 function f:filterRequest($filterKey as xs:string,$condition as xs:string ,$value as xs:integer){
             f:filterOntologies($filterKey,$condition ,$value)
     };
declare function f:filterOntologies($filterKey as xs:string,$condition as xs:string ,$value as xs:integer){
   <html>
 		<head>
       <title>Ontology Repository</title>
			</head>
			<body>
				<h2>Ontology Repository</h2>
	  		<h3>Ontologies</h3>
				<table>
			   <tr>
			    <th>Ontology</th>
         </tr>
			   { 
           for $ont in $f:ontologies return  
           <tr>
    	  	   <td> {
                    
                   f:displayFilter($ont,$filterKey,$condition,$value),$f:nl
                  
                   }
              </td>
           </tr>
         }
  			</table>
				</body>
			</html>
};

declare function f:displayFilter($ont,$filterKey,$condition,$value)
{
 if( lower-case($filterKey) = "logical axioms") then let $count:=f:countLogicalAxioms($ont) return 
              if($condition =">") 
                  then if($count > $value) 
                        then ( fn:replace (f:substring-after-last-match($ont//@ontologyIRI, '/') ,'#',' ' ) )
                  else()	
		          else              
                  if($count <$value)
                       then ( fn:replace (f:substring-after-last-match($ont//@ontologyIRI, '/') ,'#',' ' ) )
                  else()
					  else()
};

